package com.feng.leetcode;

public class Fourth {
    public static void main(String[] args) {
        int[] nums1 = {1,2};
        int[] nums2 = {3,4};
        System.out.println(findMedianSortedArrays(nums1,nums2));

    }
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int l = nums1.length + nums2.length;
        int[] arr = new int[l/2+1];
        int i=0,j=0,k=0;
        double result =0;
        while (k<arr.length){
            if (i == nums1.length){
                arr[k] = nums2[j];
                j++;
            }else if (j== nums2.length){
                arr[k] = nums1[i];
                i++;
            }else if (nums1[i] <= nums2[j]){
                arr[k] = nums1[i];
                i++;
            }else {
                arr[k] = nums2[j];
                j++;
            }
            k++;
        }
        if (l%2 == 1){
            result = arr[k-1];
        }else {
            result = (arr[k-1] + arr[k-2])/2.0;
        }
        return result;
    }
}
